﻿using MangoMes.Commont.DapperHelps;
using MangoMes.Commont.Domain.Productions;
using MangoMes.Commont.Domain.Purchase;
using MangoMes.Commont.DTOs;
using MangoMes.Commont.Infrastructures.Interfaces;
using MediatR;
using MySqlX.XDevAPI;

namespace MangoMes.Write.API.Applications.Pickinges
{
    public class PickingCreateHandler : IRequestHandler<PickingCreateCommand, ResultsDTO>
    {
        #region 仓储
        private readonly IBateRepository<Picking> PickingRepository;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="pickingRepository"></param>
        public PickingCreateHandler(IBateRepository<Picking> pickingRepository)
        {
            PickingRepository = pickingRepository;
        }
        #endregion


        #region 生产领料 新增
        /// <summary>
        /// 生产领料 新增
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<ResultsDTO> Handle(PickingCreateCommand request, CancellationToken cancellationToken)
        {
            //异常处理
            try
            {
                //初始化一个ResultDTO<ProductTypeDto>对象result，并设置Code为 0
                ResultsDTO result = new ResultsDTO() { Code = 0 };
               
                //创建DapperHelper对象 用于与数据库进行交互
                DapperHelper helper = new DapperHelper();
                //创建新实例 picking 通过对象初始化器的方式定义一些属性赋值
                Picking picking = new Picking()
                {
                    PickingId = request.PickingId,
                    PickingNode = request.PickingNode,
                    PickingName = request.PickingName,
                    PickingTime = request.PickingTime,
                    //Workid = request.Workid,
                    WorkStationName = request.WorkStationName,
                   // ClientId = request.ClientId,
                    ClientName = request.ClientName,
                    Remark = request.Remark,
                    Documents = request.Documents,
                    ClientNo = request.ClientNo,
                    WorkStations = request.WorkStations,
                    ProductionWorkOrdersNo = request.ProductionWorkOrdersNo,
                    PickingStatus =request.PickingStatus,
    };

                //连接mySql 
                string conn = "server=112.124.69.237;port=3306;uid=root;pwd=No6123666;database=MangoMesDB";

                //sql语句
                string str = "insert into picking values(@PickingId,@PickingNode,@PickingName,@PickingTime,@WorkStationName,@ClientName,@Remark,@Documents,@ClientNo,@WorkStations,@ProductionWorkOrdersNo,@PickingStatus)";

                result.Code = await PickingRepository.Add(str, picking, conn);

                return result;
            }
            catch (Exception)
            {

                throw;
            }
        }
        #endregion

    }
}
